package com.tyl.service.bill;

import com.tyl.dao.BaseDao;
import com.tyl.dao.bill.BillDao;
import com.tyl.dao.bill.BillDaoImpl;
import com.tyl.pojo.Bill;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class BillServiceImpl implements BillService {
    private BillDao billDao;

    public BillServiceImpl() {
         billDao =new BillDaoImpl();
    }

    /**
     * 获取订单列表
     * **/
    public List<Bill> getBillList(Bill bill) throws SQLException {
        Connection connection =null;
        connection = BaseDao.getConnection();
        List<Bill> billList=null;
        if (connection!=null){
            billList = billDao.getBillList(connection, bill);
        }
        BaseDao.closeResource(connection,null,null);
        return billList;
    }
    /**
     * 获取去订单id
     * **/
    public Bill getBillId(String billId) throws SQLException {
        Connection connection =null;
        connection= BaseDao.getConnection();
        Bill _billId = null;
        if (connection!=null){
            _billId= billDao.getBillId(connection, billId);
        }
        return _billId;
    }

    public int deleteBill(String billId) throws SQLException {
        Connection connection =null;
        connection= BaseDao.getConnection();
        int rs=0;
        if (connection!=null){
            rs= billDao.deleteBill(connection, billId);
        }
        BaseDao.closeResource(connection,null,null);
        return rs;
    }

    /**
     * 添加订单
     * **/
    public int addBill(Bill bill) throws SQLException {
        Connection connection =null;
        connection= BaseDao.getConnection();
        int flag=0;
        if (connection!=null){
            try {
                connection.setAutoCommit(false);
                flag = billDao.addBill(connection, bill);
                connection.commit();
            } catch (SQLException throwables) {
                connection.rollback();
                throwables.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
        }
        return flag;
    }

    public int updateBill(Bill bill) throws SQLException {
        Connection connection=null;
        connection=BaseDao.getConnection();
        int flag=0;
        if (connection!=null){
            if (billDao.updateBill(connection, bill)>0){
                flag=1;
            }
        }
        BaseDao.closeResource(connection,null,null);
        return flag;
    }
}
